import { useSearchParams } from "react-router-dom";
import { useRequest } from "ahooks";
import { getQuestionsList } from "../utils/api";
import {
  LIST_SEARCH_PARAM_KEY,
  LIST_PAGE_PARAM_KEY,
  LIST_PAGESIZE_PARAM_KEY,
  LIST_PAGESIZE,
  List_DEFAULT_PAGE
} from "../constant";

type OptionType = {
  isStar: boolean;
  isDelected: boolean;
};

const useLoadingQuestionList = (opt: Partial<OptionType>) => {
  const { isStar, isDelected } = opt;
  const [searchParams] = useSearchParams();
  const { data, loading, error,refresh } = useRequest(
    async () => {
      const page = parseInt(searchParams.get(LIST_PAGE_PARAM_KEY) || "") ||   List_DEFAULT_PAGE;
      const pageSize =
        parseInt(searchParams.get(LIST_PAGESIZE_PARAM_KEY) || "") ||
        LIST_PAGESIZE;

      const keyword = searchParams.get(LIST_SEARCH_PARAM_KEY) || "";
      const data = await getQuestionsList({ keyword, isStar, isDelected,page,pageSize });
      return data;
    },
    {
      //刷新的依赖
      refreshDeps: [searchParams],
    }
  );
  return { data, loading, error,refresh };
};
export default useLoadingQuestionList;
